<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]>      <html class="no-js"> <!--<![endif]-->
<html>
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>url scheme  以及sdk 封装， js-bridge 原理</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="" />
  </head>
  <body>
    <iframe id="iframe1"></iframe>
    <script type="module">
      const sdk = {
          invoke(url, data, success, err) {
              const iframe = document.createElement('iframe')
              iframe.style.display = 'none'
              document.body.appendChild(iframe)

              iframe.onload = () => {
                  const content = iframe.contentWindow.document.body.innerHTML
                  success(JSON.parse(content))
                  iframe.remove()
              }
              iframe.onerror = () => {
                  err()
                  iframe.remove()
              }
              iframe.src = `my-app-name://${url}?data=${JSON.stringify(data)}`
          },

          fn1(data, success, err) {
              this.invoke('api/fn1', data, success, err)
          },

          fn2(data, success, err) {
              this.invoke('api/fn2', data, success, err)
          }
      }

      // 使用
      sdk.fn1(
          {a: 10},
          (data) => { console.log('success', data) },
          () => { console.log('err') }
      )
    </script>
  </body>
</html>
